package com.karry.commons.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.karry.commons.util.HttpUtil;

import java.util.Map;

public class OpenIdService {

    /**
     * 小程序秘钥
     */
    public static String  secret;
    /**
     * 小程序 appid
     */
    public static String  appid;
    public static String  grant_type;
    /**
     * 小程序获取openId 路径
     */
    public static  String  req_url;

    public String getOpenId(String jscode){
        String openId;
        //获取openId
        String json = HttpUtil.doGet(req_url + "?" + "appid="+ appid+"&secret="+ secret+"&js_code=" + jscode + "&grant_type=" + grant_type);
        try {
            Map map = new ObjectMapper().readValue(json, Map.class);
            openId = (String)map.get("openid");
        } catch (Exception e) {
          throw new RuntimeException("获取openId失败！");
        }
        return openId;
    }
}
